using UnityEngine;

namespace GameCore
{
    public class ConfigManager : MonoBehaviour
    {
        [SerializeField] private GlobalConfig config;
        public GlobalConfig Config => config;

        public AssetBundleMode Mode => config.Mode;
        public Platform Platform => config.platform;
        public string Version => config.version;
        public bool Addressables => config.addressables;
        public int Password => config.password;

        public string Url => config.url;
        public string MetadataList => config.metadataList;
        public string HotfixDll => config.hotfixDll;
        public bool CheckFileMD5 => config.checkFileMD5;
        public bool UseFirstPackage => config.useFirstPackage;
        public bool FirstPackageInStreamingAssets => config.firstPackageInStreamingAssets;
        public int CheckAssetsRefresh => config.checkAssetsRefresh;

        public string TablePath => config.tablePath;
        public float Timeout => config.timeout;

        public string ApplicationPath
        {
            get
            {
                if (Mode == AssetBundleMode.LocalMode)
                    return Application.streamingAssetsPath;
                if (Mode == AssetBundleMode.ServerMode)
                    return Application.persistentDataPath;
                return string.Empty;
            }
        }
        public string PlatformUrl => $"{Url}AssetBundles/{Platform}";
        public string PlatformPath => $"{ApplicationPath}/AssetBundles/{Platform}";
        public string AssetBundlePath => $"{PlatformPath}/{Version}";
        public string AssetVersionUrl => $"{PlatformUrl}/{Version}";
        
        public string GetPlatformUrl(string append) => PlatformUrl + "/" + append;
        public string GetPlatformPath(string append) => PlatformPath + "/" + append;
        public string GetVersionUrl(string append) => AssetVersionUrl + "/" + append;
        public string GetVersionPath(string append) => AssetBundlePath + "/" + append;
        public string GetAssetBundlePath(AssetBundleInfo assetBundleInfo) => GetVersionPath(assetBundleInfo.PackageName + "/" + assetBundleInfo.Name);
        public string GetAssetBundleUrl(AssetBundleInfo assetBundleInfo) => GetVersionUrl(assetBundleInfo.PackageName + "/" + assetBundleInfo.Name);
    }
}